<?php

namespace app\wechat\controller;

use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Media;
use think\facade\Log;
use app\wechat\controller\Openid;

class Index
{
    public $config = [
        'app_id' => 'wx30a202bcd94caa71',
        'secret' => 'f14d74852e48be2f5065e907d6703949',
        'response_type' => 'array',
        'log' => [
            'level' => 'debug',
            'file' => __DIR__.'/wechat.log',
        ],
    ];

    public $app;

    public function __construct()
    {
//        $this->app = Factory::officialAccount($this->config);
        $this->app = app('wechat.official_account');
    }

    public function index()
    {
        $this->app->server->push(function ($message) {
            return 'hello';
        });

//        $this->handleMessage();

        $response = $this->app->server->serve();

        $response->send();
    }

    public function handleMessage()
    {
        $message = $this->app->server->getMessage();
        return 'hello world';
    }

    public function get_material_list()
    {
        return json($this->app->material->list('image'));
    }

    public function send()
    {
        $mediaId = 'OOPcGkxn6xo2ryjrh3z6uI_Ag2tbUm_OjYSgWtBn_NE';
        $media = new Media($mediaId, 'image');
        $openid = 'oxxCcv4N5Q8p0liSDNfOsXGPouus';
        $result = $this->app->customer_service->message($media)->to($openid)->send();
    }

    public function menu()
    {
        $this->app->menu->delete();
    }

}
